package com.xhj.springbootproject.util;

import com.alibaba.fastjson.JSONObject;
import com.xhj.springbootproject.constant.ResultCodeEnum;

/**
 * @author ：Xionghaijun
 * @date ：Created in 2019/7/2 4:27 PM
 * @description：
 */
public class JsonResult extends JSONObject {

    private static final long serialVersionUID = 7199322209678418037L;

    private static final String SUCCESS = "success";
    private static final String RESULT_CODE = "result_code";
    private static final String RESULT_DESC = "result_desc";
    private static final String DATA = "data";

    public JsonResult() {

    }

    public static JsonResult getInstance(Object data) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.put(SUCCESS, true);
        jsonResult.put(DATA, data);
        return jsonResult;
    }

    public static JsonResult getSuccessResult() {
        JsonResult jsonResult = new JsonResult();
        jsonResult.put(SUCCESS, true);
        return jsonResult;
    }

    public static JsonResult getFailedResult(ResultCodeEnum resultCodeEnum, String desc) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.put(SUCCESS, false);
        jsonResult.put(RESULT_CODE, resultCodeEnum.getCode());
        jsonResult.put(RESULT_DESC, desc);
        return jsonResult;
    }

    public static JsonResult getFailedResult(String resultCode, String desc) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.put(SUCCESS, false);
        jsonResult.put(RESULT_CODE, resultCode);
        jsonResult.put(RESULT_DESC, desc);
        return jsonResult;
    }

    public boolean isSuccess() {
        Object o = this.get(SUCCESS);
        return o != null && o instanceof Boolean ? (Boolean)o : false;
    }

    public JsonResult fillData(Object data) {
        this.put(DATA, data);
        return this;
    }

}
